﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using WL_Store.BLL;
using WL_Store.Model;

namespace WL_Store.Web.subpage
{
    public partial class administrator : System.Web.UI.Page
    {
        EmployeeInfoBLL bll = new EmployeeInfoBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            
            
            if (!IsPostBack)
            {
               
                BindPerList();
                BindUserInfo();
               
            }
           

        }

        /// <summary>
        /// 绑定下拉列表
        /// </summary>
        private void BindPerList()
        {
            EmployeeInfoBLL bll = new EmployeeInfoBLL();
            //读取权限信息
            DataTable dd = bll.PermissionInfo();
            //数据源赋值
            ddl_PermissionInfo.DataSource = dd;
            ddl_rank.DataSource = dd;
            //ddl_edit_jibie.DataSource = dd;
            //显示字段
            ddl_PermissionInfo.DataTextField = "Grade";
            ddl_rank.DataTextField = "Grade";
           // ddl_edit_jibie.DataTextField = "Grade";
            //值字段
            ddl_PermissionInfo.DataValueField = "GradeId";
            ddl_rank.DataValueField = "GradeId";
           // ddl_edit_jibie.DataValueField = "GradeId";
            //绑定
            ddl_PermissionInfo.DataBind();

            ddl_rank.DataBind();
            //ddl_edit_jibie.DataBind();
            ddl_PermissionInfo.Items.Insert(0, new ListItem("全部", "0"));
        }
        /// <summary>
        /// 给repeater绑定数据、多条件查询
        /// </summary>
        private void BindUserInfo() {
            //获取用户输入的信息
            string name = this.txt_search_name.Text.Trim();
            int gradeId = Convert.ToInt32(this.ddl_PermissionInfo.SelectedValue);
            int sex = Convert.ToInt32(rbl_search_sex.SelectedValue);

            DateTime? startEntrytime = null;
            if (!string.IsNullOrWhiteSpace(txt_StartEntrytime.Value.Trim()))
            {
                startEntrytime = Convert.ToDateTime(txt_StartEntrytime.Value.Trim());
            }

            DateTime? endEntrytime = null;
            if (!string.IsNullOrWhiteSpace(txt_EndEntrytime.Value.Trim()))
            {
                endEntrytime = Convert.ToDateTime(txt_EndEntrytime.Value.Trim());
            }

            EmployeeInfoSearchModel searchModel = new EmployeeInfoSearchModel() {
                Name = name,
                GradeId=gradeId,
                Sex=sex,
                StartEntrytime=startEntrytime,
                EndEntrytime=endEntrytime
                
            };



            //实例化bll层
            EmployeeInfoBLL bl = new EmployeeInfoBLL();

            int totalCount;
            //读取所有用户信息(分页)
            List<EmployeeInfoModel> list = bl.GetEmployeeInfosProc(searchModel,user_Pager.CurrentPageIndex,user_Pager.PageSize,out totalCount);
            //为分页组件的总计录进行赋值
            user_Pager.RecordCount = totalCount;

            //给数据源赋值
            rpt_admin.DataSource = list;
            //绑定
            rpt_admin.DataBind();
        }
        /// <summary>
        /// 添加按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_add_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {

                //1.获取用户输入的信息
                string number = this.txt_number.Text.Trim();
                string name = this.txt_name.Text.Trim();
                int rank = Convert.ToInt32(this.ddl_rank.SelectedValue);
                int  sex = Convert.ToInt32(this.rbt_sex.SelectedValue);
                string age = this.txt_age.Text.Trim();
                string idcard = this.txt_idcard.Text.Trim();
                string phone = this.txt_phone.Text.Trim();
                string email = this.txt_email.Text.Trim();
                string addss = this.txt_addss.Text.Trim();
                //string entryTime = this.txt_entryTime.Text.Trim();
                //string regTime = this.txt_regTime.Text.Trim();
                string loginName = this.txt_loginName.Text.Trim();
                string loginPassword = this.txt_loginPassword.Text.Trim();

                //调用业务逻辑层添加方法
                
                EmployeeInfoModel model = new EmployeeInfoModel();
                model.number = number;
                model.name = name;
                model.GradeId = rank;
                model.sex = Convert.ToBoolean(sex);
                model.age = Convert.ToInt32(age); ;
                model.idcard = idcard;
                model.phone = phone;
                model.email = email;
                model.address = addss;
                //model.entrytime = Convert.ToDateTime(entryTime);
                //model.regtime = Convert.ToDateTime(regTime);
                model.loginName = loginName;
                model.loginPassword = loginPassword;

                if (Convert.ToInt32(bll.GetAdminNumber(number)) == 0)
                {
                    if (bll.AddEmployeeInfosProc(model))
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('添加成功');location.href='administrator.aspx'</script>");

                    }
                    else
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('添加失败');</script>");

                    }

                }
                else
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>parent.layer.alert('已存在该管理员编号', {icon: 6});</script>");
                    //Response.Write("<script>layer.msg('已存在该管理员编号！', { time: 1000, shade: 0.3 });</script>");

                }
               
            }
        }

        protected void lbt_del_Command(object sender, CommandEventArgs e)
        {
           
            //判断是否是点击删除命令
            if (e.CommandName=="del")
            {
               
                string  number = e.CommandArgument.ToString();
                if (bll.DeleteEmployeeInfoProc(number))
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(),"alert", "<script>alert('删除成功')</script>");
                    BindUserInfo();
                }
                else
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('删除失败')</script>");
                    BindUserInfo();
                }
            }
           
               
                
           
           
        }
        

      

        protected void btn_search_Click(object sender, EventArgs e)
        {
           
            BindUserInfo();
        }

        protected void user_Pager_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            //为分页组件的当前页索引赋值
            user_Pager.CurrentPageIndex = e.NewPageIndex;
            //重新绑定数据
            BindUserInfo();
        }
    }
}